#!/usr/bin/perl -w

#select all active policies
#select class from policy_audit where substring_index(substring_index(info,' ',11),' ',-1) = 0;


use DBI;
use strict;


my $db="jctong";
my $host="ilwrath";
my $user="nbuadmin";
my $password="asdfjk";

my $dbh = DBI->connect ("DBI:mysql:database=$db:host=$host",$user,$password) or die "Can't connect to database: $DBI::errstr\n";

#open(INPUT,"allpolicies") || die "no input file";

my @input = `ssh root\@netbackup /usr/openv/netbackup/bin/admincmd/bppllist -allpolicies`;


my $allcolumns;
my $allvalues;
my $first=1;
my $include;
my $timestamp=time();

#while(defined(my $line=<INPUT>)){
foreach my $line (@input) {
	my($column,@values)=split(/\s/,$line);
#	print "column: $column\n";
#	print "values: @values\n";

#aggregate include lines
	if($line=~/INCLUDE (.*)/){
		my $path=$1;
		if($path=~/NEW_STREAM/){
			next;
		}else{
			$include=$include." $path";
			next;
		}
	}


	if($line=~/^CLASS / && !$first){

		$allcolumns=$allcolumns."`INCLUDE`,`timestamp`";
		$allvalues=$allvalues."\'$include\',\'$timestamp\'";

		my $sth=$dbh->prepare("insert into policy_audit (".$allcolumns.") values (".$allvalues.")");
		$sth->execute();
#		print "$allcolumns\n"; print "$allvalues\n"; exit;
		$allcolumns="";
		$allvalues="";
		$include="";
	}

	$first=0;

	$allcolumns=$allcolumns."`$column`,";
	$allvalues=$allvalues."\'@values\',";

}
